<?php

namespace app\common\job;

use app\common\model\Consulting;
use app\common\model\Member;
use app\common\service\TemplateSmsService;
use ShidianUtil\Utils\CheckUtil;
use think\queue\Job;

/**
 * 服务卡片付费提醒
 * Class SmsConsultingServer
 * @package app\common\job
 */
class SmsConsultingServer
{
    /**
     * fire是消息队列默认调用的方法
     * @param Job $job 当前的任务对象
     * @param array|mixed $data 发布任务时自定义的数据
     */
    public function fire(Job $job, $data)
    {
        $id = $data['id'] ?? null;

        $consulting = Consulting::where('id', $id)->find();
        if (is_null($consulting)) {
            $job->delete();
            return;
        }

        if ($consulting->consultingType == 1) {
            $job->delete();
            return;
        }

        $phone = Member::where('id', $consulting->memberId)->value('phone');
        if (!CheckUtil::isPhoneNumber($phone)) {
            $job->delete();
            return;
        }

        $this->getTemplateSmsService()->memberUnPay($phone, $consulting->id);

        $job->delete();
    }

    private function getTemplateSmsService(): TemplateSmsService
    {
        return app(TemplateSmsService::class);
    }
}
